﻿Namespace DoFactory.GangOfFour.Command.Structural

    ''' <summary>
    ''' MainApp startup class for Structural
    ''' Command Design Pattern.
    ''' </summary>
    Public Class MainApp

        ''' <summary>
        ''' Entry point into console application.
        ''' </summary>
        Public Shared Sub Main()

            ' Create receiver, command, and invoker
            Dim receiver As New Receiver()

            Dim command As Command = New ConcreteCommand(receiver)

            Dim invoker As New Invoker()

            ' Set and execute command
            invoker.SetCommand(command)
            invoker.ExecuteCommand()

            ' Wait for user
            'Console.ReadKey()

        End Sub

    End Class

    ''' <summary>
    ''' The 'Command' abstract class
    ''' </summary>
    MustInherit Class Command

        Protected receiver As Receiver

        ' Constructor
        Public Sub New(ByVal receiver As Receiver)
            Me.receiver = receiver
        End Sub

        Public MustOverride Sub Execute()

    End Class

    ''' <summary>
    ''' The 'ConcreteCommand' class
    ''' </summary>
    Class ConcreteCommand
        Inherits Command

        ' Constructor
        Public Sub New(ByVal receiver As Receiver)
            MyBase.New(receiver)
        End Sub

        Public Overrides Sub Execute()
            receiver.Action()
        End Sub

    End Class

    ''' <summary>
    ''' The 'Receiver' class
    ''' </summary>
    Class Receiver

        Public Sub Action()
            Console.WriteLine("Called Receiver.Action()")
        End Sub

    End Class

    ''' <summary>
    ''' The 'Invoker' class
    ''' </summary>
    Class Invoker

        Private _command As Command

        Public Sub SetCommand(ByVal command As Command)
            Me._command = command
        End Sub

        Public Sub ExecuteCommand()
            _command.Execute()
        End Sub

    End Class

End Namespace
